clear, close, clc

dx = 0.005;
L = 1;
x = dx:dx:L;

f = ones(size(x));

f(1:length(f)/2) = 0;

plot(x,f)
axis([-.1,1.1,-.1,1.1])

A0 = (2/L)*sum(f.*ones(size(x)))*dx;
fFS = A0/2;
for n=1:100
    An = (2/L)*sum(f.*cos(2*pi*n*x/L))*dx;
    Bn = (2/L)*sum(f.*sin(2*pi*n*x/L))*dx;
    fFS = fFS + An*cos(2*pi*n*x/L) + Bn*sin(2*pi*n*x/L);
end
hold on
plot(x,fFS,'r--')